<?php
class Article{
	/**
	 * Enter description here...
	 *
	 * @var Mysql
	 */
	private $db;
	
	public function __construct($db)
	{
		$this->db = $db;
	}
	
	/**
	 * 发布文章
	 *
	 * @param unknown_type $cid
	 * @param unknown_type $author
	 * @param unknown_type $title
	 * @param unknown_type $content
	 * @param unknown_type $pubtime
	 * @param unknown_type $seq
	 * @param unknown_type $afrom
	 * @param unknown_type $keywords
	 * @param unknown_type $image
	 */
	public function add($cid, $title,$content,$seq,$image)
	{
		$v = array();
		$v['cid'] = $cid;
		$v['title'] = $title;
		$v['content'] = $content;
		$v['seq'] = $seq;
		$v['image'] = $image;
		
		return $this->db->insert("article", $v, true);
	}
	
	/**
	 * 修改
	 *
	 * @param unknown_type $aid
	 * @param array $newvalue
	 */
	public function change($aid, array $newvalue)
	{
		$this->db->update("article", $newvalue, 'aid=' . $aid);
	}
	
	public function del($aid)
	{
		$this->db->delete("article", 'aid=' . $aid);
	}
	
	/**
	 * 读取文章列表
	 *
	 * @param Integer $nowp
	 * @param Integer $per_page
	 */
	public function listArticle($cid, $nowp, $per_page)
	{
		if (empty($cid))
			$where = '';
		else 
			$where = ' where t1.cid=' . $cid;
		
		 $totalNum = $this->db->fetchOne ('select count(*) from article t1 ' . $where);
        
        require_once APP . '/application/models/Util.php';
        
        $pageInfo = Util::page ($totalNum, $per_page, $nowp);
        $start    = $pageInfo ['start'];
        $list     = $this->db->fetchAll ('select t1.* from article t1 
        ' . $where .' order by seq desc,aid desc limit ' . $start . ',' . $per_page);
        
        return Array ('page' => $pageInfo['page'], 'list' => $list);
	}
	
	public function show($aid)
	{
		return $this->db->fetchRow("select * from article where aid=?", array($aid));
	}
}